﻿$(function () {
    $("#btnSearch").click(function () { search(); });
    loadFirstTables();
    loadDepartment();
});
//显示优秀民警
function loadFirstTables() {    
    $("#tableBody").html("<div class='weui-cells__title'>优秀社区民警</div>");
    $.ajax({
        type: "Get",
        url: "/Department/GetAllPoliceIsGood?_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data, function (i, item) {
                var tr = "<div class='weui-cell'>" +
                     "<div class='weui-cell__hd' style='position: relative;margin-right: 10px;'>" +
                         "<img src='/UploadFiles/" + item.picPath + "' style='width: 50px;display: block'>  " +
                     "</div>" +
                     "<div class='weui-cell__bd'>" +
                         "<p>" + item.name + "<span style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;&nbsp;&nbsp;联系电话：<a href='tel:139xxxxxxxx'>" + item.tel + "</a></span></p>" +
                         "<p style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;管辖：" + item.position + "</p>" +
                         "<p style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;理念：" + item.remarks + "</p>" +
                     "</div>" +
                 "</div>";
                $("#tableBody").append(tr);
            })
        }
    });
};

function search() {
    $("#tableBody").html("");
    var postData = {
        "Name":$("#Name").val()
    };
    $.ajax({
        type: "Post",
        url: "/Department/GetPoliceByName",
        data: postData,
        success: function (data) {
            $.each(data, function (i, item) {
                var tr = "<div class='weui-cell'>" +
                    "<div class='weui-cell__hd' style='position: relative;margin-right: 10px;'>" +
                        "<img src='/UploadFiles/" + item.picPath + "' style='width: 50px;display: block'>  " +
                    "</div>" +
                    "<div class='weui-cell__bd'>" +
                        "<p>" + item.name + "<span style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;&nbsp;&nbsp;联系电话：<a href='tel:139xxxxxxxx'>" + item.tel + "</a></span></p>" +
                        "<p style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;管辖：" + item.position + "</p>" +
                        "<p style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;理念：" + item.remarks + "</p>" +
                    "</div>" +
                "</div>";
                $("#tableBody").append(tr);
            })
        }        
    });
    $("#Name").val("");
}

//此处加载所有派出所
function loadDepartment() {
    $("#DepartmentId").html();
    var option = "";

    $.ajax({
        type: "GET",
        url: "/Department/GetAllDepIsDisplay?_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data, function (i, item) {
                option += "<option value='" + item.id + "'>" + item.name + "</option>"
            })
            $("#DepartmentId").append(option);
        }
    });
}

function changeDepartmentId(parentId) {
    if (parentId == 999) return;
    //清空二级菜单选项  
    $("#SecDepartmentId").html("");
    var option = "<option selected='' value='999'>社区</option>";

    $.ajax({
        type: "GET",
        url: "/Department/GetAllChildrenByParent?parentId=" + parentId + "&_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data, function (i, item) {
                option += "<option value='" + item.id + "'>" + item.name + "</option>"
            })
            $("#SecDepartmentId").append(option);
        }
    });

    loadTables(parentId)
}

function changeSecDepartmentId(secDarentId) {
    
    //获取当前社区所有民警
    loadTables1(secDarentId)

}

//获取当前部门所有民警
function loadTables(parentId) {
    if (parentId == 999) return;
    $("#tableBody").html("");
    $.ajax({
        type: "Get",
        url: "/Department/GetAllByDepartmentId?departmentId=" + parentId,
        success: function (data) {
            $.each(data, function (i, item) {
                var tr = "<div class='weui-cell'>" +
                    "<div class='weui-cell__hd' style='position: relative;margin-right: 10px;'>" +
                        "<img src='/UploadFiles/" + item.picPath + "' style='width: 50px;display: block'>  " +
                    "</div>" +
                    "<div class='weui-cell__bd'>" +
                        "<p>" + item.name + "<span style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;&nbsp;&nbsp;联系电话：<a href='tel:139xxxxxxxx'>" + item.tel + "</a></span></p>" +
                        "<p style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;管辖：" + item.position + "</p>" +
                        "<p style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;理念：" + item.remarks + "</p>" +
                    "</div>" +
                "</div>";
                $("#tableBody").append(tr);
            })
        }
    });
};

function loadTables1(secDarentId) {
    if (secDarentId == 999) return;
    $("#tableBody").html("");
    $.ajax({
        type: "Get",
        url: "/Department/GetAllBySecDepartmentId?secDepartmentId=" + secDarentId,
        success: function (data) {
            $.each(data, function (i, item) {
                var tr = "<div class='weui-cell'>" +
                     "<div class='weui-cell__hd' style='position: relative;margin-right: 10px;'>" +
                         "<img src='/UploadFiles/" + item.picPath + "' style='width: 50px;display: block'>  " +
                     "</div>" +
                     "<div class='weui-cell__bd'>" +
                         "<p>" + item.name + "<span style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;&nbsp;&nbsp;联系电话：<a href='tel:139xxxxxxxx'>" + item.tel + "</a></span></p>" +
                         "<p style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;管辖：" + item.position + "</p>" +
                         "<p style='font-size: 13px;color: #888888;'>&nbsp;&nbsp;理念：" + item.remarks + "</p>" +
                     "</div>" +
                 "</div>";
                $("#tableBody").append(tr);
            })
        }
    });
};